//
//  RightPageViewController.m
//  TheEer
//
//  Created by Qianfeng on 15/10/20.
//  Copyright (c) 2015年 小R. All rights reserved.
//

#import "RightPageViewController.h"
#import "HomePageViewController.h"
//#import "SDImageCache.h"

static RightPageViewController * rvc = nil;

@interface RightPageViewController (){
    UILabel * _lable;
    UITableView * _tableView;
    UISwitch * _switch;
}
@property (nonatomic ,strong) UIView * backView;

@end

@implementation RightPageViewController

+ (RightPageViewController *)sharshareSingleViewController{
    @synchronized(self){
        if (!rvc) {
            rvc = [self new];
        }
    }
    return rvc;
}

+ (void)viewAppear{
    [UIView animateWithDuration:.3 animations:^{
        rvc.backView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
        rvc.backView.alpha = 1;
    }];
}

+ (void)viewDismiss{
    [UIView animateWithDuration:.3 animations:^{
        rvc.backView.frame = CGRectMake(ScreenWidth - 250, ScreenHeight/7, 0, 0);
        rvc.backView.alpha = .2;
    }];
}

- (void)viewWillAppear:(BOOL)animated{
    [UIView animateWithDuration:.3 animations:^{
        _backView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
        _backView.alpha = 1;
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

- (void)initData{
    _dataSource = @[@"设置个人信息", @"清除缓存",@"开发者",@"关于"];
}

- (void)createView{
    UIImageView * backImage = [[UIImageView alloc] initWithFrame:self.view.bounds];
    backImage.image = [UIImage imageNamed:@"backView2.png"];
    [self.view addSubview:backImage];
    self.view.backgroundColor = RGBA(44, 147, 253, .7);
    
    _backView = [[UIView alloc] initWithFrame:CGRectMake(ScreenWidth - 250, ScreenHeight/7, 0, 0)];
    _backView.backgroundColor = [UIColor clearColor];
    _backView.alpha = .2;
    [self.view addSubview:_backView];
    [self createTopView];
    [self createTableView];
//    [self createSwitch];
}

- (void)createSwitch{
    _switch = [[UISwitch alloc] initWithFrame:CGRectMake(ScreenWidth*6/7-10, _lable.bottom+25, 0, 0)];
    [_switch addTarget:self action:@selector(switchChanged) forControlEvents:UIControlEventValueChanged];
    [_backView addSubview:_switch];
}
#pragma mark - switch
- (void)switchChanged{
    
    NSLog(@"change");
}

- (void)createTopView{
    UIImageView * imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"setting2.png"]];
    imageView.frame = CGRectMake(ScreenWidth*3 /4, 40, 60, 60);
    [_backView addSubview:imageView];
    _lable = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth/2, 70, ScreenWidth/4, 30)];
    _lable.text = @"设 置";
    _lable.textColor = [UIColor whiteColor];
    _lable.font = [UIFont systemFontOfSize:30];
    [_backView addSubview:_lable];
    
}

- (void)createTableView{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(ScreenWidth*2.4 / 7, _lable.bottom+20, ScreenWidth*4/7+25, ScreenHeight*.5)];
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.scrollEnabled = NO;
    [_backView addSubview:_tableView];
    //隐藏tableView底部的线
    [self hideTableViewBottomLine];
}

- (void)hideTableViewBottomLine {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _tableView.width, _tableView.height)];
    view.backgroundColor = [UIColor clearColor];
    _tableView.tableFooterView = view;
}

#pragma mark - uitableViewDelegate * dataSourace
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"identifier"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"identifier"];
    }
    if (indexPath.row != 0) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    cell.textLabel.text = [_dataSource objectAtIndex:indexPath.row];
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}
#pragma mark - cell->2.3.4.5
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == -1) {
        [_switch setOn:_switch.on?NO:YES animated:YES];
        [self switchChanged];
        return;
    }else if(indexPath.row == 1){
        [self cellDidClick2];
    }else if(indexPath.row == 2){
        [self showAlertxiew:@"这款app由 小别和小孟同学 倾心打造\n\n热爱iOS,热爱生活,为自己加油\n\n"];
    }else if(indexPath.row == 3){
        [self showAlertxiew:@"这是一款旅游攻略的app\n\n每一个界面都是我们精心设计的,可以浏览翻阅世界各地,亦可收藏您喜欢的内容到收藏夹,更有一些美妙的功能哦\n\n一切以用户体验为先,我们将继续努力"];
    }else if(indexPath.row == 0){
        NSNotification * notice = [NSNotification notificationWithName:@"123" object:nil userInfo:@{@"1":@"123"}];
        //发送消息
        [[NSNotificationCenter defaultCenter]postNotification:notice];
        [HomePageViewController resetViewFrame];
    }
}

- (void)exitApplication {
    UIAlertController *actionsheet = [UIAlertController alertControllerWithTitle:@"确认退出" message:nil preferredStyle:UIAlertControllerStyleAlert];
    [actionsheet addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
        exit(0);
    }]];
    [actionsheet addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        NSLog(@"取消");
    }]];
    [self presentViewController:actionsheet animated:YES completion:nil];
}

- (void)showAlertxiew:(NSString *)message{
    UIAlertView * myAlertView = [[UIAlertView alloc]initWithTitle:nil message:message delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
    myAlertView.alertViewStyle = UIAlertViewStyleDefault;
    [myAlertView show];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataSource.count;
}

-(void)cellDidClick2{
    
    NSString *sizeStr = [NSString stringWithFormat:@"%fM",[self getCacheSize]];
    
    UIAlertController *actionsheet = [UIAlertController alertControllerWithTitle:@"清除缓存" message:sizeStr preferredStyle:UIAlertControllerStyleAlert];
    
    [actionsheet addAction:[UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
//        //删除按钮
//        //1.删除sd
//        [[SDImageCache sharedImageCache] clearMemory];//清除内存缓存
//        [[SDImageCache sharedImageCache] clearDisk];//磁盘
//        //2.界面下载的缓存
//        NSString *myPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches/MyCaches"];
//        //删除
//        [[NSFileManager defaultManager] removeItemAtPath:myPath error:nil];
        
    }]];
    [actionsheet addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        NSLog(@"取消");
    }]];
    [self presentViewController:actionsheet animated:YES completion:nil];
    
    
}
- (CGFloat)getCacheSize {
    //缓存 有两类 sdwebimage 还有 每个界面保存的缓存
//    CGFloat sdSize = [[SDImageCache sharedImageCache] getSize];
//    NSString *myPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches/MyCaches"];
//    //获取文件夹中的所有的文件
//    NSArray *arr = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:myPath error:nil];
//    unsigned long long size = 0;
//    for (NSString *fileName in arr) {
//        NSString *filePath = [myPath stringByAppendingPathComponent:fileName];
//        NSDictionary *dict = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
//        size += dict.fileSize;
//    }
//    //1M = 1024 K = 1024*1024字节
//    CGFloat totalSize = (sdSize+size)/1024.0/1024.0;
//    return totalSize;
    return 0;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
